//
//  UIBarButtonItem+Extension.m
//  黑马微博2期
//
//  Created by apple on 14-10-7.
//  Copyright (c) 2014年 heima. All rights reserved.
//

#import "UIBarButtonItem+Extension.h"
#import "UIView+Extension.h"
#import <objc/message.h>

@implementation UIBarButtonItem (Extension)

- (void)setBarButton:(UIButton *)barButton
{
    // 给self通过一个“barButtonKey”的key绑定一个barButton的value，用OBJC_ASSOCIATION_RETAIN_NONATOMIC的策略。
//    objc_setAssociatedObject(<#id object#>, <#const void *key#>, <#id value#>, <#objc_AssociationPolicy policy#>)
    objc_setAssociatedObject(self, @"barButtonKey", barButton, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)barButton
{
    // 用“barButtonKey”的key从self中取值
//    objc_getAssociatedObject(<#id object#>, <#const void *key#>)
//    NSLog(@"%@", objc_getAssociatedObject(self, @"barButtonKey"));
    return objc_getAssociatedObject(self, @"barButtonKey");
}

/**
 *  创建一个item
 *
 *  @param btn         自定义按钮
 *  @param target      点击item后调用哪个对象的方法
 *  @param action      点击item后调用target的哪个方法
 *  @param image       图片
 *  @param highImage   高亮的图片
 *  @param selectImage 选中的图片
 *
 *  @return 创建完的item
 */
+ (UIBarButtonItem *)itemWithCustomBtn:(UIButton *)btn Target:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage selectImage:(NSString *)selectImage;
{
    if (!btn) { // 如果btn为空
        btn = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    if (highImage) {  // 高亮状态图片
        [btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    }
    if (selectImage) { // 选中状态图片
        [btn setImage:[UIImage imageNamed:selectImage] forState:UIControlStateSelected];
    }
    // 设置尺寸
    btn.size = btn.currentImage.size;
    
    UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
#warning 调用setBarButton方法!!
    barBtnItem.barButton = btn;
    //    [barBtnItem setBarButton:btn];
    
    return barBtnItem;

}
+ (UIBarButtonItem *)itemWithCustomBtn:(UIButton *)btn Target:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{
    return [UIBarButtonItem itemWithCustomBtn:btn Target:target action:action image:image highImage:highImage selectImage:nil];
}

+ (UIBarButtonItem *)itemWithCustomBtn:(UIButton *)btn Target:(id)target action:(SEL)action image:(NSString *)image selectImage:(NSString *)selectImage
{
    return [UIBarButtonItem itemWithCustomBtn:btn Target:target action:action image:image highImage:nil selectImage:selectImage];
}

+ (UIBarButtonItem *)itemWithCustomBtn:(UIButton *)btn Target:(id)target action:(SEL)action image:(NSString *)image
{
    return [UIBarButtonItem itemWithCustomBtn:btn Target:target action:action image:image highImage:nil selectImage:nil];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage selectImage:(NSString *)selectImage
{
    return [UIBarButtonItem itemWithCustomBtn:nil Target:target action:action image:image highImage:highImage selectImage:selectImage];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highImage:(NSString *)highImage
{
    return [UIBarButtonItem itemWithTarget:target action:action image:image highImage:highImage selectImage:nil];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image selectImage:(NSString *)selectImage
{
    return [UIBarButtonItem itemWithTarget:target action:action image:image highImage:nil selectImage:selectImage];
}

+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image
{
    return [UIBarButtonItem itemWithTarget:target action:action image:image highImage:nil selectImage:nil];
}

@end
